package com.ztf0337.mycurriculumdesign.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.entity.User;
import com.ztf0337.mycurriculumdesign.service.UserService;
import com.ztf0337.mycurriculumdesign.ui.manager.ManagerHomeActivity;

public class LoginActivity extends AppCompatActivity {

    private UserService userService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        userService = new UserService(this);
        SharedPreferences loginInfo = this.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);

        TextView tvRegister = findViewById(R.id.tvRegister);
        tvRegister.setOnClickListener(v -> {
            Intent intent = new Intent(this,RegisterActivity.class);
            this.startActivity(intent);
        });

        TextView tvNotLogin = findViewById(R.id.tvNotLogin);
        tvNotLogin.setOnClickListener(v -> {
            Intent intent = new Intent(this,MainActivity.class);
            intent.putExtra("open",1);
            this.startActivity(intent);
            finish();
        });


        boolean online = false;
        EditText etPhone = findViewById(R.id.etPhone);
        Intent intent = getIntent();
        if (intent != null) {
            if (intent.getStringExtra("phone") != null) {
                etPhone.setText(intent.getStringExtra("phone"));
            }
            online = intent.getBooleanExtra("online",false);
            Log.d("测试",online ? "真" : "假");
        }

        SharedPreferences.Editor editor = loginInfo.edit();
        if (loginInfo.getBoolean("login", false)) {
            long userId = loginInfo.getLong("user_id", -1L);
            if (userId == -1L) {
                loginInfo.edit().putBoolean("login",false).apply();
                editor.clear();   // 清空所有键值对
                editor.apply();
            } else {
                User user = userService.getUserById(userId);
                if (user == null) {
                    loginInfo.edit().putBoolean("login",false).apply();
                    editor.clear();   // 清空所有键值对
                    editor.apply();
                } else if (loginInfo.getBoolean("cbRemember", false)) {
                    loginInfo.edit().putBoolean("login",true).apply();
                } else {
                    loginInfo.edit().putBoolean("login",false).apply();
                    editor.clear();   // 清空所有键值对
                    editor.apply();
                }
            }
        }
        if (!online) {
            Intent intent1 = new Intent(this, MainActivity.class);
            Log.d("测试","自动登录");
            intent1.putExtra("open",1);
            this.startActivity(intent1);
            finish();
        }

        Button btnLogin = findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(v -> {
            EditText etPassword;
            TextView tvHint;
            CheckBox cbRemember;

            etPassword = findViewById(R.id.etPassword);
            String phone = etPhone.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            tvHint = findViewById(R.id.tvHint);
            if (phone.isBlank()) {
                tvHint.setText("请输入手机号");
                return;
            }
            if (password.isBlank()) {
                tvHint.setText("请输入密码");
                return;
            }
            if (phone.length() != 11) {
                tvHint.setText("手机格式错误");
                return;
            }
            Long login = userService.login(phone, password);
            if (login >= 0) {
                cbRemember = findViewById(R.id.cbRemember);
                loginInfo.edit().putBoolean("status",false).apply();
                loginInfo.edit().putBoolean("login",true).apply();
                loginInfo.edit().putLong("user_id",login).apply();
                loginInfo.edit().putBoolean("cbRemember",cbRemember.isChecked()).apply();
                Log.d("测试","用户查找id "+loginInfo.getLong("user_id", -1L));

                Intent intent1 = new Intent(this, MainActivity.class);
                intent1.putExtra("open",1);
                this.startActivity(intent1);
                finish();

            } else {
                if (login.equals(-1L)) {
                    tvHint.setText("用户不存在");
                }
                if (login.equals(-2L)) {
                    tvHint.setText("密码错误");
                }
                if (login.equals(-3L)) {
                    Intent intent1 = new Intent(this, ManagerHomeActivity.class);
                    intent1.putExtra("open",1);
                    Toast.makeText(this, "管理员登陆", Toast.LENGTH_SHORT).show();
                    this.startActivity(intent1);
                    finish();
                }
                if (login.equals(-4L)) {
                    Toast.makeText(this, "该账户已被封禁", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}
